package org.jeecg.modules.online.cgform.utils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.entity.OnlCgformButton;
import org.jeecg.modules.online.cgform.entity.OnlCgformEnhanceJs;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;
import org.jeecg.modules.online.cgform.enums.CgformConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* renamed from: org.jeecg.modules.online.cgform.d.d */
/* loaded from: hibernate-re-3.7.1-RC.jar:org/jeecg/modules/online/cgform/d/d.class */
public class EnhanceJsUtil {

    /* renamed from: f */
    private static final String f309f = "setup,beforeSubmit,beforeAdd,beforeEdit,afterAdd,afterEdit,beforeDelete,afterDelete,mounted,created,show,loaded";

    /* renamed from: g */
    private static final String f310g = "\\}\\s*\r*\n*\\s*";

    /* renamed from: h */
    private static final String f311h = ",";

    /* renamed from: b */
    public static final String f313b = "import";

    /* renamed from: c */
    public static final String f314c = "customImport";

    /* renamed from: d */
    public static final String f315d = "_hook";

    /* renamed from: e */
    private static final Logger f308e = LoggerFactory.getLogger(EnhanceJsUtil.class);

    /* renamed from: a */
    public static final Pattern f312a = Pattern.compile("^import\\s+(.*)\\s+from\\s+(['\"].*['\"])[;]?$");

    /* renamed from: a */
    public static String m304a(String str, String str2) {
        String str3;
        String str4 = "(" + str2 + "\\s*\\(row\\)\\s*\\{)";
        String str5 = str2 + ":function(that,row){const getAction=this._getAction,postAction=this._postAction,deleteAction=this._deleteAction,useMessage = this._useMessage;";
        String m298b = m298b(str, f310g + str4, "}," + str5);
        if (m298b == null) {
            str3 = m295c(str, str4, str5);
        } else {
            str3 = m298b;
        }
        return m303a(str3, str2, (String) null);
    }

    /* renamed from: a */
    public static String m303a(String str, String str2, String str3) {
        String str4;
        String str5 = "(" + oConvertUtils.getString(str3) + str2 + "\\s*\\(\\)\\s*\\{)";
        String str6 = str2 + ":function(that){const getAction=this._getAction,postAction=this._postAction,deleteAction=this._deleteAction,useMessage = this._useMessage;";
        String m298b = m298b(str, f310g + str5, "}," + str6);
        if (m298b == null) {
            str4 = m295c(str, str5, str6);
        } else {
            str4 = m298b;
        }
        return str4;
    }

    /* renamed from: b */
    public static String m298b(String str, String str2, String str3) {
        Matcher matcher = Pattern.compile(str2).matcher(str);
        if (matcher.find()) {
            return str.replace(matcher.group(0), str3);
        }
        return null;
    }

    /* renamed from: c */
    public static String m295c(String str, String str2, String str3) {
        String m298b = m298b(str, str2, str3);
        if (m298b != null) {
            return m298b;
        }
        return str;
    }

    /* renamed from: a */
    public static String m302a(String str, List<OnlCgformButton> list) {
        return "class OnlineEnhanceJs{constructor(getAction,postAction,deleteAction){this._getAction=getAction;this._postAction=postAction;this._deleteAction=deleteAction;}" + str + "}";
    }

    /* renamed from: b */
    public static String m299b(String str, String str2) {
        String str3;
        String str4 = "([ \\t]+" + str2 + "\\s*\\(\\)\\s*\\{)";
        String str5 = str2 + ":function(that,event){";
        String m298b = m298b(str, f310g + str4, "}," + str5);
        if (m298b == null) {
            str3 = m295c(str, str4, str5);
        } else {
            str3 = m298b;
        }
        return str3;
    }

    /* renamed from: a */
    public static String m305a(String str) {
        return "function OnlineEnhanceJs(getAction,postAction,deleteAction){return {_getAction:getAction,_postAction:postAction,_deleteAction:deleteAction," + str + "}}";
    }

    /* renamed from: b */
    public static String m297b(String str, List<OnlCgformButton> list) {
        return "function OnlineEnhanceJs(getAction,postAction,deleteAction){return {_getAction:getAction,_postAction:postAction,_deleteAction:deleteAction," + m294c(str, list) + "}}";
    }

    /* renamed from: c */
    public static String m294c(String str, List<OnlCgformButton> list) {
        String[] split;
        String m304a;
        String m300b = m300b(str);
        if (list != null) {
            for (OnlCgformButton onlCgformButton : list) {
                String buttonCode = onlCgformButton.getButtonCode();
                if ("link".equals(onlCgformButton.getButtonStyle())) {
                    m300b = m304a(m304a(m300b, buttonCode), buttonCode + f315d);
                } else if ("button".equals(onlCgformButton.getButtonStyle()) || CgformUtil.f271aq.equals(onlCgformButton.getButtonStyle())) {
                    m300b = m303a(m303a(m300b, buttonCode, (String) null), buttonCode + f315d, (String) null);
                }
            }
        }
        for (String str2 : f309f.split(",")) {
            if ("setup,beforeAdd,afterAdd,mounted,created,show,loaded".indexOf(str2) >= 0) {
                m304a = m303a(m300b, str2, (String) null);
            } else {
                m304a = m304a(m300b, str2);
            }
            m300b = m304a;
        }
        return m300b;
    }

    /* renamed from: a */
    public static void m301a(OnlCgformEnhanceJs onlCgformEnhanceJs, String str, List<OnlCgformField> list) {
        if (onlCgformEnhanceJs == null || oConvertUtils.isEmpty(onlCgformEnhanceJs.getCgJs())) {
            return;
        }
        String str2 = " " + onlCgformEnhanceJs.getCgJs();
        f308e.debug("one enhanceJs begin==> " + str2);
        if (Pattern.compile("(\\s{1}onlChange\\s*\\(\\)\\s*\\{)").matcher(str2).find()) {
            f308e.debug("---JS 增强转换-main--enhanceJsFunctionName----onlChange");
            str2 = m303a(str2, CgformConstant.f373h, "\\s{1}");
            for (OnlCgformField onlCgformField : list) {
                str2 = m299b(str2, onlCgformField.getDbFieldName());
            }
        }
        f308e.debug("one enhanceJs end==> " + str2);
        onlCgformEnhanceJs.setCgJs(str2);
    }

    /* renamed from: b */
    public static void m296b(OnlCgformEnhanceJs onlCgformEnhanceJs, String str, List<OnlCgformField> list) {
        if (onlCgformEnhanceJs == null || oConvertUtils.isEmpty(onlCgformEnhanceJs.getCgJs())) {
            return;
        }
        String cgJs = onlCgformEnhanceJs.getCgJs();
        String str2 = str + "_" + CgformConstant.f373h;
        if (Pattern.compile("(" + str2 + "\\s*\\(\\)\\s*\\{)").matcher(cgJs).find()) {
            cgJs = m303a(cgJs, str2, (String) null);
            for (OnlCgformField onlCgformField : list) {
                cgJs = m299b(cgJs, onlCgformField.getDbFieldName());
            }
        }
        onlCgformEnhanceJs.setCgJs(cgJs);
    }

    /* renamed from: b */
    private static String m300b(String str) {
        String[] split = str.split("\n");
        for (int i = 0; i < split.length; i++) {
            String trim = split[i].trim();
            if (trim.startsWith("import")) {
                Matcher matcher = f312a.matcher(trim);
                if (matcher.find()) {
                    split[i] = split[i].replace(trim, String.format("const %s = %s(%s)", matcher.group(1), f314c, matcher.group(2)));
                }
            }
        }
        return String.join("\n", split);
    }
}
